import axios from 'axios'
const userBaseUrl = import.meta.env.VITE_BASE_URL

// 封装用户请求方式
function userHttp({
  url,
  method,
  user_name,
  user_password,
  user_type,
  user_onlinestatus,
  user_other,
}) {
  return axios({
    url: userBaseUrl + url,
    method,
    data: {
      user_name,
      user_password,
      user_type,
      user_onlinestatus,
      user_other,
    },
  })
}

// 获取所用用户分页显示
const getAllUserhttp = (page, size, keyword) => {
  if (!keyword) {
    keyword = ''
  }
  return userHttp({
    url: `/user?page=${page}&size=${size}&keyword=${keyword}`,
    method: 'get',
  })
}

// 根据id获取用户信息
const getOneUserhttp = (user_id) => {
  return userHttp({
    url: `/user/${user_id}`,
    method: 'get',
  })
}

// 添加交管用户请求
const postTrafficUserHttp = (user_name, user_password) => {
  return userHttp({
    url: '/user',
    method: 'post',
    user_name,
    user_password,
    user_type: 1,
    user_onlinestatus: 0,
    user_other: 0,
  })
}

// 修改用户账号请求
const putUserHttp = (user_id, user_name, user_password, user_onlinestatus) => {
  if (!user_onlinestatus) {
    user_onlinestatus = '1'
  }
  return userHttp({
    url: `/user/${user_id}`,
    method: 'put',
    user_name,
    user_password,
    user_onlinestatus,
  })
}

// 修改登录状态
const putStateUserHttp = (
  user_id,
  user_name,
  user_password,
  user_onlinestatus
) => {
  if (!user_onlinestatus) {
    user_onlinestatus = '0'
  }
  return userHttp({
    url: `/user/${user_id}`,
    method: 'put',
    user_name,
    user_password,
    user_onlinestatus,
  })
}

// 删除用户请求
const deleteUserHttp = (user_id) => {
  return userHttp({
    url: `/user/${user_id}`,
    method: 'delete',
  })
}

// 导出
export {
  getAllUserhttp,
  getOneUserhttp,
  postTrafficUserHttp,
  putUserHttp,
  deleteUserHttp,
  putStateUserHttp,
}
